package com.itlxd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itlxd.entity.Dish;
import com.itlxd.entity.SetmealDish;
import com.itlxd.mapper.DishMapper;
import com.itlxd.mapper.SetmealDishMapper;
import com.itlxd.service.IDishService;
import com.itlxd.service.ISetmealDishService;
import com.itlxd.vo.DishVO;
import org.omg.PortableInterceptor.DISCARDING;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 套餐菜品关系 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Transactional
@Service
public class SetmealDishServiceImpl implements ISetmealDishService {

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private IDishService dishService;

    @Override
    public Integer insertBatch(List<SetmealDish> setmealDishes) {
        int count = 0;
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishMapper.insert(setmealDish);
            count++;
        }
        return count;
    }

    @Override
    public List<SetmealDish> listSetmealDishes(String setmealId) {
        LambdaQueryWrapper<SetmealDish> lambdaQuery = new LambdaQueryWrapper();
        lambdaQuery.eq(null != setmealId, SetmealDish::getSetmealId, setmealId);
        return setmealDishMapper.selectList(lambdaQuery);
    }

    @Override
    public Integer updateBatchSetmealDishes(List<SetmealDish> setmealDishes) {

        int count = 0;
        for (SetmealDish setmealDish : setmealDishes) {
            LambdaQueryWrapper<SetmealDish> lambdaQuery = new LambdaQueryWrapper();
            lambdaQuery.eq(SetmealDish::getDishId,setmealDish.getDishId())
                    .eq(SetmealDish::getSetmealId, setmealDish.getSetmealId());
            setmealDishMapper.update(setmealDish, lambdaQuery);
            count++;
        }
        return count;
    }

    @Override
    public Integer deleteBatchBySetmealId(List<Long> setmealIds) {
        int count = 0;
        for (Long setmealId : setmealIds) {
            LambdaQueryWrapper<SetmealDish> lambdaQuery = new LambdaQueryWrapper<>();
            lambdaQuery.eq(SetmealDish::getSetmealId, setmealId);
            setmealDishMapper.delete(lambdaQuery);
            count++;
        }

        return count;
    }

    @Override
    public List<DishVO> listDishesBySetmealId(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> lambdaQuery = new LambdaQueryWrapper();
        lambdaQuery.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(lambdaQuery);

        List<DishVO> dishVOList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishList) {
            DishVO dishVO = dishService.getOneById(Long.parseLong(setmealDish.getDishId()));
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
